package com.chen.until;

import com.chen.dto.PageData;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

public class DownLoadExcelUntils {

    public static  void downLoadExcel(
            HttpServletRequest request,
            HttpServletResponse response,
            List<LinkedHashMap>fansData,
            String[] headers,
            String tname
    ) throws IOException {

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        // 创建样式对象
        HSSFCellStyle redStyle = workbook.createCellStyle();
        // 设置背景色  13是黄色  14是粉色
        redStyle.setFillForegroundColor((short) 13);
        redStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        redStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
        //创建文本样式对象
        HSSFFont redFont = workbook.createFont();
        //粗体
        redFont.setBold(true);
        //设置字体高度
        redFont.setFontHeightInPoints((short) 16);
        //将文本样式放入样式对象中
        redStyle.setFont(redFont);
        String fileName = tname + MyUntils.getShortId() + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        //用sheet创建第一行表格
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            //用row创建一列列数据  即 一小格
            HSSFCell cell = row.createCell(i);
            //写入表头数据
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            //设置每一列的宽度
            sheet.setColumnWidth(i, 5800);
            //设置每一行的高度
            row.setHeightInPoints(23);
            //设置单元格样式
            cell.setCellStyle(redStyle);
            //填入数据
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        List<String> fileRwoName = new ArrayList<>();
        LinkedHashMap hashMap = fansData.get(0);
        Iterator x = hashMap.entrySet().iterator();
        while (x.hasNext()) {
            Map.Entry entry = (java.util.Map.Entry)x.next();
            fileRwoName.add(entry.getKey().toString());
        }
        for (int i = 0; i < fansData.size(); i++) {
            HSSFRow row1 = sheet.createRow(rowNum);
            //为每一行创建行高
            row1.setHeightInPoints(18);
            // 创建样式对象  为序号居中
            HSSFCellStyle redStyle2 = workbook.createCellStyle();
            // 居中
            redStyle2.setAlignment(HorizontalAlignment.CENTER);
            HSSFCell cell = row1.createCell(0);
            cell.setCellStyle(redStyle2);
            //序号
            cell.setCellValue(i + 1);
            for (int j = 0; j < fileRwoName.size(); j++) {
                row1.createCell(j+1).setCellValue(fansData.get(i).get(fileRwoName.get(j)).toString());
            }
            rowNum++;
        }
        response.setHeader("Content-disposition", "attachment");
        response.setHeader("filename", fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());


    }
}
